// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-25 11:28
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain.preFixTrain;

public class XorQueries {
    /**
     * 子数组累加异或查询
     */
    public int[] xorQueries(int[] arr, int[][] queries) {
        /*-----定义前缀和数组-----*/
        int[] preFix = new int[arr.length];
        preFix[0] = arr[0];
        for (int i = 1; i < arr.length; i++) {
            preFix[i] = preFix[i - 1] ^ arr[i];
        }
        /*-----根据前缀和数组进行异或查询-----*/
        int[] res = new int[queries.length];
        int k = 0;
        for (int[] query : queries) {
            int start = query[0], end = query[1];
            if (start > 0) {
                res[k] = preFix[end] ^ preFix[start - 1];
            } else {
                res[k] = preFix[end];
            }
            k++;
        }
        return res;
    }
}
